#################################################################################
### Title: Are People Willing to Trade Away Democracy for Desirable Outcomes? ###
### Authors: Jonathan A. Chu, Scott Williamson, Eddy S. F. Yeung              ###
### Content: Marginal rate of substitution analysis                           ###
### Date: September 20, 2025                                                  ###
#################################################################################

### Set-up ----
## Clean the working environment and set the working directory
rm(list = ls())
setwd("~/Desktop/democracy_tradeoff/replication") # change to your own working directory

## Load the required packages
library(tidyverse)

## Read the cleaned and recoded datasets
df_US <- read.csv("df_US.csv")
df_JP <- read.csv("df_JP.csv")
df_EG <- read.csv("df_EG.csv")
df_IN <- read.csv("df_IN.csv")
df_IT <- read.csv("df_IT.csv")
df_TH <- read.csv("df_TH.csv")

## Merge the datasets
df_cj <- bind_rows(df_US, df_JP, df_EG, df_IN, df_IT, df_TH)

## Reorder the factors
# Leader selection (binary)
df_cj$free_election <- ifelse(df_cj$Leader.Selection == "Free and fair elections", "Free and fair elections", "No free and fair elections")
df_cj$free_election <- 
  factor(df_cj$free_election,
         levels = c("Free and fair elections", "No free and fair elections"))

# Civil liberties
df_cj$`Civil Liberties` <- 
  factor(df_cj$Civil.Liberties,
         levels = c("Free", "Partially free", "Repressed"))

# Institutional checks
df_cj$`Institutional Checks` <- 
  factor(df_cj$Leader.Constraints,
         levels = c("Constrained", "Partially constrained", "Unconstrained"))

# National economy
df_cj$`National Economy` <- 
  factor(df_cj$National.Economy,
         levels = c("High income", "Middle income", "Low income"))

# Respondent wealth
df_cj$`Respondent Wealth` <- 
  factor(df_cj$Respondent.Wealth,
         levels = c("Wealthy", "Average", "Poor"))

# Public safety
df_cj$`Public Safety` <- 
  factor(df_cj$Public.Safety,
         levels = c("Very safe", "Somewhat safe", "Somewhat dangerous", "Very dangerous"))

# Corruption in politics
df_cj$`Corruption in Politics` <- 
  factor(df_cj$Corruption.in.Politics,
         levels = c("Rare", "Occasional", "Prevalent"))

# Health care
df_cj$`Health Care` <- 
  factor(df_cj$Health.Care,
         levels = c("Mostly accessible", "For the privileged"))

# Minority treatment
df_cj$`Minority Treatment` <- 
  factor(df_cj$Minority.Treatment,
         levels = c("Fairly treated", "Sometimes unfair", "Mostly unfair"))

# Respondent identity
df_cj$`Respondent Identity` <- 
  factor(df_cj$Respondent.Identity,
         levels = c("Majority", "Second largest", "Minority"))

# Country
df_cj$country <- 
  factor(df_cj$country, 
         levels = c("US", "JP", "EG", "IN", "IT", "TH"),
         labels = c("United States", "Japan", "Egypt", "India", "Italy", "Thailand"))

## Assign values to each attribute level
df_cj <- df_cj %>% 
  mutate(dem_election = case_when(
    free_election == "Free and fair elections"    ~ 1,
    free_election == "No free and fair elections" ~ 0
  )) %>%   
  mutate(dem_civil = case_when(
    `Civil Liberties` == "Free"           ~ 1,
    `Civil Liberties` == "Partially free" ~ 0.5,
    `Civil Liberties` == "Repressed"      ~ 0
  )) %>%  
  mutate(dem_constr = case_when(
    `Institutional Checks` == "Constrained"           ~ 1,
    `Institutional Checks` == "Partially constrained" ~ 0.5,
    `Institutional Checks` == "Unconstrained"         ~ 0
  )) %>% 
  mutate(outcome_econ = case_when(
    `National Economy` == "High income"   ~ 1,
    `National Economy` == "Middle income" ~ 0.5,
    `National Economy` == "Low income"    ~ 0
  )) %>% 
  mutate(outcome_wealth = case_when(
    `Respondent Wealth` == "Wealthy" ~ 1,
    `Respondent Wealth` == "Average" ~ 0.5,
    `Respondent Wealth` == "Poor"    ~ 0
  )) %>% 
  mutate(outcome_safety = case_when(
    `Public Safety` == "Very safe"          ~ 1,
    `Public Safety` == "Somewhat safe"      ~ 2/3,
    `Public Safety` == "Somewhat dangerous" ~ 1/3,
    `Public Safety` == "Very dangerous"     ~ 0
  )) %>% 
  mutate(outcome_corrupt = case_when(
    `Corruption in Politics` == "Rare"       ~ 1,
    `Corruption in Politics` == "Occasional" ~ 0.5,
    `Corruption in Politics` == "Prevalent"  ~ 0
  )) %>% 
  mutate(outcome_health = case_when(
    `Health Care` == "Mostly accessible"  ~ 1,
    `Health Care` == "For the privileged" ~ 0,
  )) %>% 
  mutate(outcome_minority = case_when(
    `Minority Treatment` == "Fairly treated"   ~ 1,
    `Minority Treatment` == "Sometimes unfair" ~ 0.5,
    `Minority Treatment` == "Mostly unfair"    ~ 0
  )) %>% 
  mutate(outcome_identity = case_when(
    `Respondent Identity` == "Majority"       ~ 1,
    `Respondent Identity` == "Second largest" ~ 0.5,
    `Respondent Identity` == "Minority"       ~ 0
  ))

## Reshape the data from long to wide format
df_structural <- reshape(data = df_cj,
                         idvar = c("id", "task"),
                         timevar = "profile",
                         direction = "wide")

## Compute the differences between the two levels for each attribute
df_structural$dem_election <- df_structural$dem_election.2 - df_structural$dem_election.1
df_structural$dem_civil <- df_structural$dem_civil.2 - df_structural$dem_civil.1
df_structural$dem_constr <- df_structural$dem_constr.2 - df_structural$dem_constr.1
df_structural$outcome_econ <- df_structural$outcome_econ.2 - df_structural$outcome_econ.1
df_structural$outcome_wealth <- df_structural$outcome_wealth.2 - df_structural$outcome_wealth.1
df_structural$outcome_safety <- df_structural$outcome_safety.2 - df_structural$outcome_safety.1
df_structural$outcome_corrupt <- df_structural$outcome_corrupt.2 - df_structural$outcome_corrupt.1
df_structural$outcome_health <- df_structural$outcome_health.2 - df_structural$outcome_health.1
df_structural$outcome_minority <- df_structural$outcome_minority.2 - df_structural$outcome_minority.1
df_structural$outcome_identity <- df_structural$outcome_identity.2 - df_structural$outcome_identity.1

## Create a variable that captures which country the respondent selected
df_structural$selected <- df_structural$selected.2 - df_structural$selected.1

### MRS for leadership selection (Table S2, column 1) ----
## Keep relevant variables only
df_structural_election <- df_structural %>% 
  select(id, country.1, selected, dem_election,
         outcome_econ, outcome_wealth, outcome_safety, outcome_corrupt, 
         outcome_health, outcome_minority, outcome_identity)

## Indicate whether the respondent chose Country A
df_structural_election$country_A <- 
  ifelse(df_structural_election$selected == 1, 1, 0)

## Flip all signs if dem_election = -1 (same logic as Graham & Svolik's data reshaping so that all Candidate A's are not more democratic than Candidate B's)
df_structural_election$selected <- 
  ifelse(df_structural_election$dem_election == -1, df_structural_election$selected * -1, df_structural_election$selected)
df_structural_election$outcome_econ <- 
  ifelse(df_structural_election$dem_election == -1, df_structural_election$outcome_econ * -1, df_structural_election$outcome_econ)
df_structural_election$outcome_wealth <- 
  ifelse(df_structural_election$dem_election == -1, df_structural_election$outcome_wealth * -1, df_structural_election$outcome_wealth)
df_structural_election$outcome_safety <- 
  ifelse(df_structural_election$dem_election == -1, df_structural_election$outcome_safety * -1, df_structural_election$outcome_safety)
df_structural_election$outcome_corrupt <- 
  ifelse(df_structural_election$dem_election == -1, df_structural_election$outcome_corrupt * -1, df_structural_election$outcome_corrupt)
df_structural_election$outcome_health <- 
  ifelse(df_structural_election$dem_election == -1, df_structural_election$outcome_health * -1, df_structural_election$outcome_health)
df_structural_election$outcome_minority <- 
  ifelse(df_structural_election$dem_election == -1, df_structural_election$outcome_minority * -1, df_structural_election$outcome_minority)
df_structural_election$outcome_identity <- 
  ifelse(df_structural_election$dem_election == -1, df_structural_election$outcome_identity * -1, df_structural_election$outcome_identity)
df_structural_election$country_A <- 
  ifelse(df_structural_election$dem_election == -1, 1 - df_structural_election$country_A, df_structural_election$country_A)
df_structural_election$dem_election <- 
  ifelse(df_structural_election$dem_election == -1, df_structural_election$dem_election * -1, df_structural_election$dem_election)

## Estimate the random utility model of discrete choice with sample fixed effects
temp <- 
  glm(country_A ~ country.1 + dem_election + 
        outcome_econ + outcome_wealth + outcome_safety + outcome_corrupt + 
        outcome_health + outcome_minority + outcome_identity, 
      family = binomial(link = "logit"), 
      data = df_structural_election)

## Calculate MRS of free elections for each desirable outcome
temp$coefficients["outcome_econ"] / temp$coefficients["dem_election"]
temp$coefficients["outcome_wealth"] / temp$coefficients["dem_election"]
temp$coefficients["outcome_safety"] / temp$coefficients["dem_election"]
temp$coefficients["outcome_corrupt"] / temp$coefficients["dem_election"]
temp$coefficients["outcome_health"] / temp$coefficients["dem_election"]
temp$coefficients["outcome_minority"] / temp$coefficients["dem_election"]
temp$coefficients["outcome_identity"] / temp$coefficients["dem_election"]

### MRS for civil liberties (Table S2, column 2) ----
## Keep relevant variables only
df_structural_civil <- df_structural %>% 
  select(id, country.1, selected, dem_civil,
         outcome_econ, outcome_wealth, outcome_safety, outcome_corrupt, 
         outcome_health, outcome_minority, outcome_identity)

## Indicate whether the respondent chose Country A
df_structural_civil$country_A <- 
  ifelse(df_structural_civil$selected == 1, 1, 0)

## Flip all signs if dem_civil < 0 (same logic as Graham & Svolik's data reshaping so that all Candidate A's are not more democratic than Candidate B's)
df_structural_civil$selected <- 
  ifelse(df_structural_civil$dem_civil < 0, df_structural_civil$selected * -1, df_structural_civil$selected)
df_structural_civil$outcome_econ <- 
  ifelse(df_structural_civil$dem_civil < 0, df_structural_civil$outcome_econ * -1, df_structural_civil$outcome_econ)
df_structural_civil$outcome_wealth <- 
  ifelse(df_structural_civil$dem_civil < 0, df_structural_civil$outcome_wealth * -1, df_structural_civil$outcome_wealth)
df_structural_civil$outcome_safety <- 
  ifelse(df_structural_civil$dem_civil < 0, df_structural_civil$outcome_safety * -1, df_structural_civil$outcome_safety)
df_structural_civil$outcome_corrupt <- 
  ifelse(df_structural_civil$dem_civil < 0, df_structural_civil$outcome_corrupt * -1, df_structural_civil$outcome_corrupt)
df_structural_civil$outcome_health <- 
  ifelse(df_structural_civil$dem_civil < 0, df_structural_civil$outcome_health * -1, df_structural_civil$outcome_health)
df_structural_civil$outcome_minority <- 
  ifelse(df_structural_civil$dem_civil < 0, df_structural_civil$outcome_minority * -1, df_structural_civil$outcome_minority)
df_structural_civil$outcome_identity <- 
  ifelse(df_structural_civil$dem_civil < 0, df_structural_civil$outcome_identity * -1, df_structural_civil$outcome_identity)
df_structural_civil$country_A <- 
  ifelse(df_structural_civil$dem_civil < 0, 1 - df_structural_civil$country_A, df_structural_civil$country_A)
df_structural_civil$dem_civil <- 
  ifelse(df_structural_civil$dem_civil < 0, df_structural_civil$dem_civil * -1, df_structural_civil$dem_civil)

## Estimate the random utility model of discrete choice with sample fixed effects
temp <- 
  glm(country_A ~ country.1 + dem_civil + 
        outcome_econ + outcome_wealth + outcome_safety + outcome_corrupt + 
        outcome_health + outcome_minority + outcome_identity, 
      family = binomial(link = "logit"), 
      data = df_structural_civil)

## Calculate MRS of free elections for each desirable outcome
temp$coefficients["outcome_econ"] / temp$coefficients["dem_civil"]
temp$coefficients["outcome_wealth"] / temp$coefficients["dem_civil"]
temp$coefficients["outcome_safety"] / temp$coefficients["dem_civil"]
temp$coefficients["outcome_corrupt"] / temp$coefficients["dem_civil"]
temp$coefficients["outcome_health"] / temp$coefficients["dem_civil"]
temp$coefficients["outcome_minority"] / temp$coefficients["dem_civil"]
temp$coefficients["outcome_identity"] / temp$coefficients["dem_civil"]

### MRS for institutional checks (Table S2, column 3) ----
## Keep relevant variables only
df_structural_constr <- df_structural %>% 
  select(id, country.1, selected, dem_constr,
         outcome_econ, outcome_wealth, outcome_safety, outcome_corrupt, 
         outcome_health, outcome_minority, outcome_identity)

## Indicate whether the respondent chose Country A
df_structural_constr$country_A <- 
  ifelse(df_structural_constr$selected == 1, 1, 0)

## Flip all signs if dem_constr < 0 (same logic as Graham & Svolik's data reshaping so that all Candidate A's are not more democratic than Candidate B's)
df_structural_constr$selected <- 
  ifelse(df_structural_constr$dem_constr == -1, df_structural_constr$selected * -1, df_structural_constr$selected)
df_structural_constr$outcome_econ <- 
  ifelse(df_structural_constr$dem_constr == -1, df_structural_constr$outcome_econ * -1, df_structural_constr$outcome_econ)
df_structural_constr$outcome_wealth <- 
  ifelse(df_structural_constr$dem_constr == -1, df_structural_constr$outcome_wealth * -1, df_structural_constr$outcome_wealth)
df_structural_constr$outcome_safety <- 
  ifelse(df_structural_constr$dem_constr == -1, df_structural_constr$outcome_safety * -1, df_structural_constr$outcome_safety)
df_structural_constr$outcome_corrupt <- 
  ifelse(df_structural_constr$dem_constr == -1, df_structural_constr$outcome_corrupt * -1, df_structural_constr$outcome_corrupt)
df_structural_constr$outcome_health <- 
  ifelse(df_structural_constr$dem_constr == -1, df_structural_constr$outcome_health * -1, df_structural_constr$outcome_health)
df_structural_constr$outcome_minority <- 
  ifelse(df_structural_constr$dem_constr == -1, df_structural_constr$outcome_minority * -1, df_structural_constr$outcome_minority)
df_structural_constr$outcome_identity <- 
  ifelse(df_structural_constr$dem_constr == -1, df_structural_constr$outcome_identity * -1, df_structural_constr$outcome_identity)
df_structural_constr$country_A <- 
  ifelse(df_structural_constr$dem_constr == -1, 1 - df_structural_constr$country_A, df_structural_constr$country_A)
df_structural_constr$dem_constr <- 
  ifelse(df_structural_constr$dem_constr == -1, df_structural_constr$dem_constr * -1, df_structural_constr$dem_constr)

## Estimate the random utility model of discrete choice with sample fixed effects
temp <- 
  glm(country_A ~ country.1 + dem_constr + 
        outcome_econ + outcome_wealth + outcome_safety + outcome_corrupt + 
        outcome_health + outcome_minority + outcome_identity, 
      family = binomial(link = "logit"), 
      data = df_structural_constr)

## Calculate MRS of free elections for each desirable outcome
temp$coefficients["outcome_econ"] / temp$coefficients["dem_constr"]
temp$coefficients["outcome_wealth"] / temp$coefficients["dem_constr"]
temp$coefficients["outcome_safety"] / temp$coefficients["dem_constr"]
temp$coefficients["outcome_corrupt"] / temp$coefficients["dem_constr"]
temp$coefficients["outcome_health"] / temp$coefficients["dem_constr"]
temp$coefficients["outcome_minority"] / temp$coefficients["dem_constr"]
temp$coefficients["outcome_identity"] / temp$coefficients["dem_constr"]
